package cn.fjsay.java.demo1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class JdbcDemo4 {
    Connection conn = null;
    PreparedStatement psmt = null;
    ResultSet rs = null;

    public List<Person> getPersonList() {
        List<Person> list = new ArrayList<Person>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/persondb", "root", "root");
            System.out.println(conn == null ? "连接失败" : "连接成功");
            String sql = "select * from t_person";
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
            while (rs.next()) {
                int pid = rs.getInt(1);
                String pname = rs.getString(2);
                String pwd = rs.getString(3);
                String gender = rs.getString(4);
                String address = rs.getString(5);
                Person person = new Person(pid, pname, pwd, gender, address);
                list.add(person);
            }
            return list;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                psmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public Person getPersonById(int pid) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/persondb", "root", "root");
            System.out.println(conn == null ? "连接失败" : "连接成功");
            String sql = "select * from t_person where pid=?";
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1, pid);
            rs = psmt.executeQuery();
            while (rs.next()) {
                String pname = rs.getString(2);
                String pwd = rs.getString(3);
                String gender = rs.getString(4);
                String address = rs.getString(5);
                return new Person(pid, pname, pwd, gender, address);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                psmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        JdbcDemo4 d4 = new JdbcDemo4();
        List<Person> persons = d4.getPersonList();
        for (Person person : persons) {
            System.out.println(person);
        }
    }
}